package bintree.leetcode;

/**
 * 判断平衡树
 * @author yuisama
 * @date 2022/05/12 21:34
 **/
public class Num110_IsBalancedTree {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        // 求左树高度
        int left = height(root.left);
        // 求右树高度
        int right = height(root.right);
        int abs = Math.abs(left - right);
        return abs <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

    public int height(TreeNode root) {
        return root == null ? 0 : 1 + Math.max(height(root.left),height(root.right));
    }
}